//
//  UnreadMessageModel.m
//  XV
//
//  Created by apple on 8.11.19.
//  Copyright © 2019 XR. All rights reserved.
//

#import "UnreadMessageModel.h"
#import "MJExtension.h"


@implementation UnreadMessageModel

//userDefaults
#define kUnreadMessageInfo @"UnreadMessageInfo"

MJCodingImplementation

//未读消息数量
-(NSInteger)getUnreadCount
{
    return self.sessionUnreadCount+self.systemUnreadCount+self.customSystemUnreadCount;
}


//获取IM未读消息model(用户标识，如果为nil则使用默认)
+(UnreadMessageModel*)read:(NSString*)uid
{
    NSString* str = kUnreadMessageInfo;
    if (kStringIsEmpty(uid)==NO)
    {
        str = [NSString stringWithFormat:@"%@%@",kUnreadMessageInfo,uid];
    }
    
    // 解档
    NSData *infoData = [[NSUserDefaults standardUserDefaults] objectForKey:str];
    UnreadMessageModel *info = [NSKeyedUnarchiver unarchiveObjectWithData:infoData];
    
    if (info==nil) {
        info = [[UnreadMessageModel alloc]init];
    }
    
    return info;
}

//保存IM未读消息
-(void)write:(NSString*)uid
{
    NSString* str = kUnreadMessageInfo;
    if (kStringIsEmpty(uid)==NO)
    {
        str = [NSString stringWithFormat:@"%@%@",kUnreadMessageInfo,uid];
    }
    
    // 归档
    NSData *infoData = [NSKeyedArchiver archivedDataWithRootObject:self];
    [[NSUserDefaults standardUserDefaults] setObject:infoData forKey:str];
}

//删除IM未读消息(用户标识，如果为nil则使用默认)
+(void)remove:(NSString*)uid
{
    NSString* str = kUnreadMessageInfo;
    if (kStringIsEmpty(uid)==NO)
    {
        str = [NSString stringWithFormat:@"%@%@",kUnreadMessageInfo,uid];
    }
    
    // 清空归档
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:str];
}

@end
